var express = require('express');
const query = require('express/lib/middleware/query');
var router = express.Router();
var db = require('../model/index')

/* 查询所有商品 goods_class*/
router.get('/goods/getallgoods', function (req, res, next) {
    console.log(req.query)
    let mysql = ''
    if (req.query.classid == 0) {
        mysql = `SELECT * FROM goods where NAME LIKE '%${req.query.keyWord}%' LIMIT ${req.query.startNum},${req.query.num}`;
    } else {
        mysql = `SELECT * FROM goods where classid=${req.query.classid} AND NAME LIKE '%${req.query.keyWord}%' LIMIT ${req.query.startNum},${req.query.num}`;
    }
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询商品列表失败，请稍后重试',
                data: err
            })
        }
        res.json({
            code: 200,
            msg: '查询商品列表成功',
            data: data
        })
    })
});

/* 查询购物车信息*/
router.get('/goods/getcart', function (req, res, next) {
    let mysql = `SELECT * FROM cart AS t1 LEFT JOIN goods AS t2 ON t1.goodid=t2.id;`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询商品列表失败，请稍后重试',
                data: err
            })
        }
        res.json({
            code: 200,
            msg: '查询商品列表成功',
            data: data
        })
    })
});

/* 加入购物车*/
router.post('/goods/addcart', function (req, res, next) {
    console.log(req.body)
    let mysql = `INSERT INTO cart VALUES(NULL,${req.body.goodid},${req.body.addNum},'${req.body.addSize}','${req.body.addCla}');`;
    db(mysql, function (err, data) {
        console.log(err)
        if (err) {
            return res.json({
                code: 500,
                msg: '加购失败，请稍后重试',
                data: err
            })
        }
        console.log(data)
        res.json({
            code: 200,
            msg: '加购成功',
            data: data
        })
    })
});

/* 加入购物车*/
router.post('/goods/editcart', function (req, res, next) {
    let idstr = String(req.body)
    let mysql = `DELETE FROM cart WHERE goodid IN (${idstr});`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '删除失败，请稍后重试',
                data: err
            })
        }
        res.json({
            code: 200,
            msg: '删除成功',
            data: data
        })
    })
});

/* 商品分类 */
router.get('/goods/getallspfl', function (req, res, next) {
    let mysql = `SELECT * FROM goods_class`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询成功',
            data: data
        })
    })
});
/* 商品分类，是否上架滑动开关 /goods/spflsfsj */
router.post('/goods/spflsfsj', function (req, res, next) {
    let mysql = `UPDATE goods_class SET sfsj='${req.body.sfsj}' 
  WHERE id='${req.body.id}';`
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '上架失败,请稍后重试',
            })
        }
        res.json({
            code: 200,
            msg: '上架成功',
            data: data
        })
    })
});
/* 筛选 */
router.post('/goods/sxuan/feippai', function (req, res, next) {
    console.log(req)

    let mysql = `SELECT * FROM goods WHERE NAME='${req.pp}'`

    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询失败,请稍后重试',
            })
        }
        res.json({
            code: 200,
            msg: '查询成功',
            data: data,
        })
    })
});
/* 商品分类，首页推荐滑动开关 /goods/spflsytj */
router.post('/goods/spflsytj', function (req, res, next) {
    let mysql = `UPDATE goods_class SET sytj='${req.body.sytj}' 
  WHERE id='${req.body.id}';`
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '上架失败,请稍后重试',
            })
        }
        res.json({
            code: 200,
            msg: '上架成功',
            data: data,
        })
    })
});
/* 商品分类，数据删除 /goods/spflsc */
router.post('/goods/spflsc', function (req, res, next) {
    let mysql = `DELETE FROM goods_class 
  WHERE id='${req.body.id}';`
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '删除失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '删除成功',
            data: data
        })
    })
});
/* 商品分类，数据添加 /goods/spfltj */
router.post('/goods/spfltj', function (req, res, next) {
    let mysql = `INSERT INTO goods_class VALUES 
  (NULL,'${req.body.fl}',NULL,
  '${req.body.jb}','${req.body.spsl}','${req.body.sfsj}',
  '${req.body.sytj}','${req.body.px}');`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '添加失败,请稍后重试',
                data: err
            })
        }
        res.json({
            code: 200,
            msg: '添加成功',
            data: data

        })
    })
});
/* 商品分类，数据编辑 /goods/spflbj */
router.post('/goods/spflbj', function (req, res, next) {
    if (req.body.sfsj) {
        req.body.sfsj = '是'
    } else {
        req.body.sfsj = '否'
    }
    if (req.body.sytj) {
        req.body.sytj = '是'
    } else {
        req.body.sytj = '否'
    }
    let mysql = `UPDATE goods_class SET 
  fl='${req.body.fl}',jb='${req.body.jb}',
  spsl='${req.body.spsl}',sfsj='${req.body.sfsj}',
  sytj='${req.body.sytj}',px='${req.body.px}'
  WHERE id='${req.body.id}';`
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '编辑失败，请稍后重试',
                data: err
            })
        }
        res.json({
            code: 200,
            msg: '编辑成功',
            data: data
        })
    })
});

/* 商品审核 goods_examine*/
router.get('/goods/getallspsh', function (req, res, next) {
    let mysql = `SELECT * FROM goods_examine`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询失败，请稍后重试'
            })
        }
        /* 筛选 */
        if (JSON.stringify(req.query) != '{}') {
            for (let i = 0; i < data.length; i++) {
                if (data[i].spmc.indexOf(req.query.spmc) == -1) {
                    data.splice(i, 1);
                    i--;
                } else if (req.query.pp != '全部') {
                    if (data[i].pp != req.query.pp) {
                        data.splice(i, 1);
                        i--;
                    };
                } else if (req.query.zt != '全部') {
                    if (data[i].zt != req.query.zt) {
                        data.splice(i, 1);
                        i--;
                    };
                }
            }
        };
        res.json({
            code: 200,
            msg: '查询成功',
            data: data
        })
    })
});
/* 商品审核 ，数据添加 /goods/spshtj */
router.post('/goods/spshtj', function (req, res, next) {
    let mysql = `INSERT INTO goods_examine VALUES 
  (NULL,NULL,'${req.body.spmc}',
  '${req.body.hh}','${req.body.jg}','${req.body.fl}',
  '${req.body.pp}','待审核');`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '添加失败,请稍后重试',
                data: err
            })
        }
        res.json({
            code: 200,
            msg: '添加成功',
            data: data
        })
    })
});
/* 商品审核 ，数据删除 /goods/spshsc */
router.post('/goods/spshsc', function (req, res, next) {
    let mysql = `DELETE FROM goods_examine 
  WHERE id='${req.body.id}';`
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '删除失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '删除成功',
            data: data
        })
    })
});
/* 商品审核 ，数据审核 /goods/spshsh */
router.post('/goods/spshsh', function (req, res, next) {
    let mysql = `UPDATE goods_examine SET 
  zt='${req.body.zt}' WHERE id='${req.body.id}';`
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '编辑失败，请稍后重试',
                data: err
            })
        }
        res.json({
            code: 200,
            msg: '编辑成功',
            data: data
        })
    })
});
/* 商品审核 ，数据编辑 /goods/spshbj */
router.post('/goods/spshbj', function (req, res, next) {
    let mysql = `UPDATE goods_examine SET 
  spmc='${req.body.spmc}',hh='${req.body.hh}',
  jg='${req.body.jg}',fl='${req.body.fl}',
  pp='${req.body.pp}'
  WHERE id='${req.body.id}';`
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '编辑失败，请稍后重试',
                data: err
            })
        }
        res.json({
            code: 200,
            msg: '编辑成功',
            data: data
        })
    })
});

//查询所有商品评价
router.get('/goods/commentgoods', function (req, res, next) {
    let mysql = `SELECT * FROM goods_sppj`
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询商品列表失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询商品列表成功',
            data: data
        })
    })
});

//添加商品
router.post('/goods/addBanner', function (req, res, next) {
    console.log(req)
    let mysql = `INSERT INTO goods (id,sphh,NAME,fzzl,fzjj,zffs,symj,dsdp,zmpp,img,sfby,xssl) VALUES (NULL,'${req.body.sphh}','${req.body.name}','${req.body.fzzl}','${req.body.fzjj}','${req.body.zffs}','${req.body.symj}','${req.body.dsdp}' , '${req.body.zmpp}', '${req.body.img}','${req.body.sfby}' ,'${req.body.xssl}')`
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '添加失败，请稍后再试'
            })
        }
        res.json({
            code: 200,
            msg: '添加成功'
        })
    })
})

//更改
router.post('/goods/changegoods', function (req, res, next) {
    console.log(req)
    let mysql = `UPDATE goods SET sphh='${req.body.sphh}',
  NAME='${req.body.name}',
  symj='${req.body.symj}',
  spjg='${req.body.spjg}',
  fzzl='${req.body.fzzl}',
  zmpp='${req.body.zmpp}',
  sfby='${req.body.sfby}' WHERE id=${req.body.id}`
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '更改添加失败，请稍后再试'
            })
        }
        res.json({
            code: 200,
            msg: '更改成功'
        })
    })
})



router.post('/goods/commentgoods/change', function (req, res, next) {
    console.log(req)
    let mysql = `UPDATE goods_sppj SET spmc='${req.body.spmc}' ,
              yhnc = '${req.body.yhnc}',
              sppf = '${req.body.sppf}',
              spjg = '${req.body.spjg}',
              ipdz = '${req.body.ipdz}',
              gwcs = '${req.body.gwcs}',
              pjnr = '${req.body.pjnr}'
          WHERE id = ${req.body.id}
          `
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '更改添加失败，请稍后再试'
            })
        }
        res.json({
            code: 200,
            msg: '更改成功'
        })
    })
})


/*删除评价信息  */
// operate_banner
router.post('/goods/delBanner', function (req, res, next) {
    console.log(req.body.id)
    let mysql = `
          DELETE FROM goods_sppj WHERE id = '${req.body.id}';
          `
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '删除失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '删除成功',
        })
    })
});

module.exports = router;